package main

import (
	"fmt"
	"log"

	//"github.com/sirupsen/logrus"
	"github.com/gin-gonic/gin"
	"time"
)

func main() {
	start := time.Now()

	//gin.SetMode(gin.ReleaseMode)
	//router := gin.Default()

	gin.DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
		str := fmt.Sprintf("[%s] %v %v %v %v", time.Now().Format("2006-01-02 15:04:05.000"), httpMethod, absolutePath, handlerName, nuHandlers)
		log.Println(str)
	}

	router := gin.New()
	router.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
		// your custom format
		return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",
			param.ClientIP,
			param.TimeStamp.Format("2006-01-02 15:04:05.000"),
			param.Method,
			param.Path,
			param.Request.Proto,
			param.StatusCode,
			param.Latency,
			param.Request.UserAgent(),
			param.ErrorMessage,
		)
	}))
	router.Use(gin.Recovery())

	//router.Static("/assets", "./assets")
	//router.StaticFS("/more_static", http.Dir("my_file_system"))
	router.StaticFile("/aaa", "./static/index.html")

	end := time.Now()
	fmt.Printf("### 1启动 | 耗时：%v \n", end.Sub(start))
	// 监听并在 0.0.0.0:8080 上启动服务
	err := router.Run(":8080")
	if err != nil {
		fmt.Printf("### 启动失败 %s \n", err)
	}
}
